﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class ContactListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void btnResetSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
        public void LoadContactListData2(int companyid)
        {
            SqlCommand myCommand;
            String sqlSELECT = "SELECT * FROM [rs_tblContact]";
            String sqlWHERE = " where companyid=@companyid";

            List<string> paramlist = new List<string>();
            paramlist.Add("@companyid");

            if (txtContactID.Text != "")
            {
                sqlWHERE = sqlWHERE + " and contactid=@contactid";
                paramlist.Add("@contactid");
            }
            else
            {

                if (txtFirstName.Text != "")
                {
                    sqlWHERE = sqlWHERE + " and (CHARINDEX(@firstname, firstname)>0)";
                    paramlist.Add("@firstname");
                }
                if (txtLastName.Text != "")
                {
                    sqlWHERE = sqlWHERE + " and (CHARINDEX(@lastname, lastname)>0)";
                    paramlist.Add("@lastname");
                }
            }

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT + " " + sqlWHERE + "  ORDER BY LastName, FirstName ", myConnection);

            for (int i = 0; i < paramlist.Count(); i++)
            {
                if (paramlist[i] == "@companyid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@companyid";
                    param.Value = companyid;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@contactid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@contactid";
                    param.Value = int.Parse(txtContactID.Text);
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@firstname")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@firstname";
                    param.Value = txtFirstName.Text.Trim();
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@lastname")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@lastname";
                    param.Value = txtLastName.Text.Trim();
                    myCommand.Parameters.Add(param);
                }
            }


            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvContacts.DataSource = ds;
            gvContacts.DataBind();
            lblContactCount.Text = ds.Tables[0].Rows.Count.ToString();
            myConnection.Close();
        }
        protected void btnAddNewContact_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                Response.Redirect("~/RS/SBM/RSContactDetail.aspx?action=add");
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvContacts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvContacts.PageIndex = e.NewPageIndex;
            gvContacts.DataBind();
        }

        protected void gvContacts_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvContacts.DataSource = dataView;
                gvContacts.DataBind();
            }
        }

    }
}